﻿// <copyright file="BlockMapEntry.cs" company="Sinclair Community College">
// Copyright 2010 Sinclair Community College
// </copyright>
// <originalAuthor>Patrick S. Seymour (patrick.seymour@gmail.com)</originalAuthor>

namespace SinclairCC.AppV
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// This class represents a Block Map Entry in an App-V SFT file.
    /// </summary>
    /// <remarks>
    /// Each Block Map entry is constructed from the header for that entry and the
    /// list of 32-bit offsets to the data blocks.
    /// </remarks>
    public class BlockMapEntry
    {
        /// <summary>
        /// This field contains the first 4 bytes from the full 16 bytes GUID from
        /// the Id field in the entry found in the Directory Map Subsection.
        /// </summary>
        private byte[] directoryEntryId;

        /// <summary>
        /// This field stores the number of data block offsets that follow. Each data
        /// block offset is the offset to the data block, relative to the start of the file.
        /// </summary>
        private int dataBlockCount;

        /// <summary>
        /// The list of 32-bit offsets for the data blocks for this block map entry.
        /// </summary>
        private int[] offsetList;

        /// <summary>
        /// Initializes a new instance of the BlockMapEntry class.
        /// </summary>
        /// <param name="fileReader">
        /// A binary file reader that points to the SFT file.
        /// </param>
        public BlockMapEntry(System.IO.BinaryReader fileReader)
        {
            this.directoryEntryId = fileReader.ReadBytes(4);
            this.dataBlockCount = fileReader.ReadInt32();

            this.offsetList = new int[this.dataBlockCount];
            for (int i = 0; i < this.dataBlockCount; i++)
            {
                this.offsetList[i] = fileReader.ReadInt32();
            }
        }

        /// <summary>
        /// Gets the first 4 bytes from the full 16-byte GUID from the Id field in
        /// the entry found in the Directory Map Subsection.
        /// </summary>
        public byte[] DirectoryEntryId
        {
            get { return this.directoryEntryId; }
        }

        /// <summary>
        /// Gets the number of data block offsets that follow the header.
        /// </summary>
        public int DataBlockCount
        {
            get { return this.dataBlockCount; }
        }

        /// <summary>
        /// Gets the list of 32-bit offsets for the data blocks for this block map entry.
        /// </summary>
        public int[] Offsets
        {
            get { return this.offsetList; }
        }
    }
}
